// shopPages/meiShi/meiShi.js
var sliderWidth = 38;
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    page: 1,
    seller_list: [],
    more_status: true,
    imgUrls: [],
    jiuggs: [],
    // 选项卡
    tabs: ["为我推荐", "距离", "销量"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    w_list: [],
    dots_f: false,
    currentSwiper: 0,
    currentSwiper1: 0
  },
  // 轮播指示点
  swiperChange: function(e) {
    this.setData({
      currentSwiper1: e.detail.current,
    })
  },
  swiperchange: function(e) {
    this.setData({
      currentSwiper: e.detail.current,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var page = that.data.page;
    console.log(options);
    that.setData({
      p_cat_id: options.cat_id,
      page: page + 1
    });
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    var url2 = "/mall/mallIndexFood"
    var params2 = {
      pid: options.cat_id,
      city_code: app.globalData.currentCode,
      latitude: app.globalData.latitude,
      longitude: app.globalData.longitude,
      pageno: page,
      state: 0
    };
    app.HttpPostSend(url2, params2, function(res) {

      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        // console.log('测试' + res.cat_list)
        // console.log('图片' + res.banner_list.data.img)
        that.setData({
          imgUrls: res.banner_list,
          grids: res.cat_list,
          seller_list: res.seller,
          seller_discount_zone: res.seller_discount_zone
        })
      }
    });
  },
  toGoodsList: function(e) {
    var cat_id = e.currentTarget.dataset.cat_id;
    wx.navigateTo({
      url: '../../shopPages/storeList/storeList?cat_id=' + cat_id + '&show_start_free=1',
    })
  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id,
      seller_list: [],
      page: 1,
      more_status: true,
    });
    this.getSeller(1, e.currentTarget.id);
  },
  getSeller: function(pageno, state) {
    var that = this;
    var new_list = that.data.seller_list;
    var url2 = "/mall/mallSecondSeller"
    var params2 = {
      pid: that.data.p_cat_id,
      city_code: app.globalData.currentCode,
      latitude: app.globalData.latitude,
      longitude: app.globalData.longitude,
      pageno: pageno,
      state: state
    };
    var more_status = that.data.more_status;
    app.HttpPostSend(url2, params2, function(res) {
      wx.hideLoading();
      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        if (res.seller.length < 10) {
          more_status = false;
        } else {
          more_status = true;
          var page = that.data.page;
          page += 1;
          that.setData({
            page: page
          });
        }
        var list = res.seller;
        for (let i = 0; i < list.length; i++) {
          new_list.push(list[i]);
        }
        console.log(new_list);
        that.setData({
          seller_list: new_list,
          more_status: more_status
        });

      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    console.log(that.data.more_status);
    if (that.data.more_status == true) {
      var that = this;
      // 显示加载图标
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1
      that.getSeller(that.data.page, that.data.activeIndex);

    } else {
      wx.hideLoading();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  toGoodsDetail: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  toMyMall: function(e) {
    app.globalData.SwitchTabParams.v_id = e.currentTarget.dataset.uid;
    if (e.currentTarget.dataset.food_mall > 0) {
      wx.navigateTo({
        url: '/shopPages/shopxq/shopxq',
      })
    } else {
      wx.navigateTo({
        url: '/shopPages/personalShop/personalShop'
      })
    }
    // wx.navigateTo({
    //   url: '../../pages/myMall/myMall',
    // })
    // wx.navigateTo({
    //   url: '/shopPages/shopxq/shopxq',
    // })
  }
})